//
//  LXHomeItemTableViewCell.m
//  EnterpriseEdition
//
//  Created by ypj on 2022/7/6.
//

#import "LXHomeItemTableViewCell.h"
#import "QuanXianModel.h"
#import "HomeCategoryItemCell.h"

#define kItemHeight 75 * ([UIScreen mainScreen].bounds.size.width/375.0f)
#define kItemCount 4

@interface LXHomeItemTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic) UICollectionView *collectionView;

@end


@implementation LXHomeItemTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    
    if (self) {
        
        
        UILabel *tipslabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 13, 100, 20)];
        tipslabel.textColor = ColorWith333333;
        self.tipslabel = tipslabel;
        tipslabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
        [self.contentView addSubview:tipslabel];
        
       
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        
        flowLayout.itemSize = CGSizeMake((kScreenWidth - 70)/kItemCount,kItemHeight);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 15, 15, 15);
        flowLayout.minimumLineSpacing= 10;
        flowLayout.minimumInteritemSpacing = 10;
        
        self.collectionView.showsVerticalScrollIndicator = NO;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.backgroundColor = UIColor.whiteColor;
        [self.contentView addSubview:self.collectionView];
        
        self.collectionView.scrollEnabled = NO;
        self.collectionView.frame = CGRectMake(0, tipslabel.Y_height + 10, kScreenWidth, 30);
        [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeCategoryItemCell  class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([HomeCategoryItemCell class])];

        

    }
    return self;
}



- (void)setDataAry:(NSArray *)dataAry{

    _dataAry = dataAry;

    self.tipslabel.text = @"";
    
    CGFloat height = [LXHomeItemTableViewCell getCellHeightWithDataAry:dataAry];
    self.collectionView.height = height;
    [self.collectionView reloadData];
    
}

+(CGFloat)getCellHeightWithDataAry:(NSArray*)dataAry
{
    CGFloat height = dataAry.count/kItemCount *kItemHeight;
    if (dataAry.count%kItemCount !=0 ){
        height = (dataAry.count/kItemCount +1) *kItemHeight ;
    }
    CGFloat count = dataAry.count/4.0;
    return height + 50 + (8 * ceil(count));
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataAry.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    QuanXianSmallModel *item = self.dataAry[indexPath.row];
    HomeCategoryItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeCategoryItemCell class]) forIndexPath:indexPath];
    cell.titleLb.text = [NSString stringWithFormat:@"%@",item.name];
    [cell.iconImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HTTP_URL,[NSString isChinese:item.icon]]] placeholderImage:Image(@"default_icon")];
    return cell;
   
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    QuanXianSmallModel *item = self.dataAry[indexPath.row];
    if (self.ToSendValue) {
        self.ToSendValue(item);
    }
    
}



@end
//@implementation LXJingYingCollectionCell
//
//-(instancetype)initWithFrame:(CGRect)frame{
//    self=[super initWithFrame:frame];
//    if (self){
//        [self setUI];
//    }
//    return self;
//}
//
//- (void)setUI{
//
//    self.topLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height/2)];
//    self.topLabel.textColor= [UIColor colorWithHexString:@"#3C6CFC"];
//    self.topLabel.font= Fond20;
//    self.topLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:20];
//    self.topLabel.textAlignment = NSTextAlignmentCenter;
//   [self addSubview:self.topLabel];
//
//    self.bottomLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, MaxY(self.topLabel), self.width, self.height/2)];
//    self.bottomLabel.textColor= Color999;
//    self.bottomLabel.font= Fond13;
//    self.bottomLabel.textAlignment = NSTextAlignmentCenter;
//   [self addSubview:self.bottomLabel];
//
//}




//@end


